package sdl2_impl

import (
	"github.com/gonutz/go-sdl2/sdl"
	"github.com/gonutz/go-sdl2/sdl_ttf"
	"unsafe"
)

type Fonts struct {
	Header, SubHeader, Text *ttf.Font
}

func (f *Fonts) Init() error {
	ttf.Init() // TODO check int result here (see doc for meanings)
	f.Header = createFont(35)
	f.SubHeader = createFont(30)
	f.Text = createFont(25)
	if f.Header == nil || f.SubHeader == nil || f.Text == nil {
		return EUnableToLoadFonts{}
	}
	return nil
}

type EUnableToLoadFonts struct{}

func (_ EUnableToLoadFonts) Error() string {
	return "Could not load fonts."
}

func createFont(size int) *ttf.Font {
	font, _ := ttf.OpenFontRW(fromBytes(SaboFilledTtf), size)
	return font
}

func fromBytes(data []byte) *sdl.RWops {
	return sdl.RWFromMem(unsafe.Pointer(&data[0]), len(data))
}

func (f *Fonts) Release() {
	ttf.Quit()
}
